/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sabayframework.mem.impl;

import org.sabayframework.activity.Activity;
import org.sabayframework.channel.Channel;
import org.sabayframework.channel.ram.ArrayChannel;
import org.sabayframework.channel.ram.LinkedListChannel;
import org.sabayframework.mem.Memorizable;
import org.sabayframework.mem.Transaction;
import org.sabayframework.mem.impl.TxRequest.Type;
import org.sabayframework.queue.ExecutionQueue;

public class TransactionImpl implements Transaction {

	private final long id;
	private final ExecutionQueue executionQueue;
	private volatile Status status;
	private TxActivity txActivity;
		
	public TransactionImpl(long id, ExecutionQueue executionQueue) {
		super();
		this.id = id;
		this.executionQueue = executionQueue;
		this.status = Status.ACTIVE;
	}

	public void submitProcessingActivity(TxActivity processActivity){
		this.txActivity = processActivity;
	}
	@Override
	public long getId() {
		return id;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void contains(Channel<Long> input, Channel<Boolean> output) {
		try{
			TxRequest r = new TxRequest();
			r.type = Type.CONTAINS;
			Activity act = txActivity.getReadActivity(r); 
			executionQueue.submit(act, input, output);
		} catch (Throwable e) {
			input.fail(e);
			output.fail(e);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public <I extends Memorizable, O> void execute(Class<I> objectClass,
			Activity<I, O> activity, Channel<Long> input, Channel<O> output) {
		try{
			LinkedListChannel<I> rch=new LinkedListChannel<I>();
			TxRequest r = new TxRequest();
			r.type = Type.READ;
			r.objectClass = objectClass;
			Activity readActivity = txActivity.getReadActivity(r); 
			executionQueue.submit(readActivity, input, rch);
			executionQueue.submit(activity, rch, output);
		} catch (Throwable e) {
			input.fail(e);
			output.fail(e);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public <T extends Memorizable> void read(Class<T> objectClass, Channel<Long> input, Channel<T> output) {
		try{
			TxRequest r = new TxRequest();
			r.type = Type.READ;
			r.objectClass = objectClass;
			Activity readActivity = txActivity.getReadActivity(r); 
			executionQueue.submit(readActivity, input, output);
		} catch (Throwable e) {
			input.fail(e);
			output.fail(e);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public <T extends Memorizable> Channel<Void> write(Channel<T> input) {
		LinkedListChannel<Void> output = new LinkedListChannel<Void>();
		try{
			TxRequest r = new TxRequest();
			r.type = Type.WRITE;
			Activity writeActivity = txActivity.getWriteActivity(r); 
			executionQueue.submit(writeActivity, input, output);
		} catch (Throwable e) {
			input.fail(e);
			output.fail(e);
		}
		return output;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Channel<Void> delete(Channel<Long> input) {
		LinkedListChannel<Void> output = new LinkedListChannel<Void>();
		try{
			TxRequest r = new TxRequest();
			r.type = Type.DELETE;
			Activity writeActivity = txActivity.getWriteActivity(r); 
			executionQueue.submit(writeActivity, input, output);
		} catch (Throwable e) {
			input.fail(e);
			output.fail(e);
		}
		return output;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Channel<Void> commit() {
		ArrayChannel<Type> input = new ArrayChannel<Type>(Type.COMMIT);
		input.close();
		LinkedListChannel<Void> output = new LinkedListChannel<Void>();
		try{
			TxRequest r = new TxRequest();
			r.type = Type.COMMIT;
			Activity writeActivity = txActivity.getWriteActivity(r); 
			executionQueue.submit(writeActivity, input, output);
		} catch (Throwable e) {
			input.fail(e);
			output.fail(e);
		}
		return output;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Channel<Void> rollback() {
		ArrayChannel<Type> input = new ArrayChannel<Type>(Type.ROLLBACK);
		input.close();
		LinkedListChannel<Void> output = new LinkedListChannel<Void>();
		try{
			TxRequest r = new TxRequest();
			r.type = Type.ROLLBACK;
			Activity writeActivity = txActivity.getWriteActivity(r); 
			executionQueue.submit(writeActivity, input, output);
		} catch (Throwable e) {
			input.fail(e);
			output.fail(e);
		}
		return output;
	}

	@Override
	public Status getStatus() {
		return status;
	}

	protected void setStatus(Status status) {
		this.status = status;
	}
	
}
